package com.ygqh.baby.controller.mobile;

import com.ygqh.baby.ao.ProductStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResponseEntity;
import com.ygqh.baby.handler.HomePageProductHandler;
import com.ygqh.baby.handler.ProductSolrHandler;
import com.ygqh.baby.model.HomePageProduct;
import com.ygqh.baby.model.HomeProductData;
import com.ygqh.baby.model.ProductListRequestParam;
import com.ygqh.baby.model.YgProduct4ListModel;
import com.ygqh.baby.service.YgProductService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author admin
 */
@Controller
@RequestMapping("/solr/product")
public class ProductSolrController extends BaseController {

	@Autowired
	private YgProductService ygProductService;
	@Autowired
	private HomePageProductHandler homePageProductHandler;
	@Autowired
	private ProductSolrHandler productSolrHandler;

	@RequestMapping("/list")
	@ResponseBody
	public ResponseEntity list(ProductListRequestParam param) {
		initRequestParam(param);
		SolrDocumentList documentList = productSolrHandler.productList(param);
		if (documentList == null || documentList.size() == 0) {
			List<YgProduct4ListModel> findList = ygProductService.findList(param);
			ygProductService.processProductPriceForYjdian(findList);
			return ResponseEntity.success(findList);
		}
		productSolrHandler.processSharePriceForYjdian(documentList);
		return ResponseEntity.success(documentList);
	}

	private void initRequestParam(ProductListRequestParam param) {
		param.setSalesPlatform(SALES_PLATFORM.name());
		if (param.getSaleStatus() == null) {
			param.setSaleStatus(ProductStatus.OnSale);
		}
	}

	/**
	 * 首页商品
	 *
	 * @return
	 */
	@RequestMapping("homePageProduct")
	@ResponseBody
	public ResponseEntity homePageProduct(ProductListRequestParam param) {
		initRequestParam(param);
		HomePageProduct homePageProduct = homePageProductHandler.getHomePageProduct();
		if (homePageProduct == null || CollectionUtils.isEmpty(homePageProduct.getpIdList())) {
			return ResponseEntity.success();
		}
		List<Long> productIdList = homePageProduct.getpIdList();
		Map<Long, List<HomeProductData>> collect = homePageProduct.getpList().stream()
				.collect(Collectors.groupingBy(HomeProductData::getProductId));
		SolrDocumentList documentList = productSolrHandler.productListByIdList(productIdList, param);
		if (documentList == null || documentList.size() == 0) {
			List<YgProduct4ListModel> findList = ygProductService.findProduct4ListModelByIds(
					new QueryInfo(param.getStart(), (param.getStart() + param.getLimit())),
					productIdList, SALES_PLATFORM);
			if (CollectionUtils.isNotEmpty(findList)) {
				ygProductService.processProductPriceForYjdian(findList);
				findList.sort(Comparator.comparingInt(o -> collect.get(o.getId()).get(0).getSortNum()));
			}
			return ResponseEntity.success(findList);
		}
		productSolrHandler.processSharePriceForYjdian(documentList);
		documentList.sort(Comparator.comparing(o ->
				collect.get(Long.valueOf(o.get("id").toString())).get(0).getSortNum()));
		return ResponseEntity.success(documentList);
	}

	/**
	 * 按二级分类分组获取最新上架商品
	 *
	 * @param param
	 * @return
	 */
	@RequestMapping("listBySecondaryCategory")
	@ResponseBody
	public ResponseEntity listBySecondaryCategory(ProductListRequestParam param) {
		initRequestParam(param);
		HomePageProduct homePageProduct = homePageProductHandler.getHomePageProduct();
		if (homePageProduct != null && CollectionUtils.isNotEmpty(homePageProduct.getScIdList())) {
			param.setSkipScIdList(homePageProduct.getScIdList());
		}
		SolrDocumentList documentList = productSolrHandler.productListGroupBySecondaryCategory(param);
		if (documentList == null || documentList.size() == 0) {
			List<YgProduct4ListModel> findList = ygProductService.findListGroupBySecondCategory(param);
			ygProductService.processProductPriceForYjdian(findList);
			return ResponseEntity.success(findList);
		}
		productSolrHandler.processSharePriceForYjdian(documentList);
		return ResponseEntity.success(documentList);
	}

	/**
	 * 按款式分组获取最新商家商品
	 *
	 * @param param
	 * @return
	 */
	@RequestMapping("listByProductGroup")
	@ResponseBody
	public ResponseEntity listByProductGroup(ProductListRequestParam param) {
		initRequestParam(param);
		SolrDocumentList documentList = productSolrHandler.productListGroupByProductGroup(param);
		if (documentList == null || documentList.size() == 0) {
			List<YgProduct4ListModel> findList = ygProductService.findListGroupByProductGroup(param);
			ygProductService.processProductPriceForYjdian(findList);
			return ResponseEntity.success(findList);
		}
		productSolrHandler.processSharePriceForYjdian(documentList);
		return ResponseEntity.success(documentList);
	}
}
